package com.ycx.furns.web;

import com.ycx.furns.entity.Furn;
import com.ycx.furns.entity.Page;
import com.ycx.furns.service.FurnService;
import com.ycx.furns.service.impl.FurnServiceImpl;
import com.ycx.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/customerServlet")
public class CustomerServlet extends BasicServlet{
    FurnService furnService = new FurnServiceImpl();

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Furn> page = furnService.getPage(pageNo, pageSize);
        req.setAttribute("page", page);
        req.setAttribute("action", "page");
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);

    }

    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String name = req.getParameter("name");
        if(name == null){
            name = "";
        }
        Page<Furn> page = furnService.getPageByName(name, pageNo, pageSize);
        if(page.getItems() == null){
            req.setAttribute("msg", "搜索到 0 条记录");
        }
        req.setAttribute("page", page);
        req.setAttribute("action", "pageByName");
        req.setAttribute("name" , name);
        req.getRequestDispatcher("/views/customer/index.jsp").forward(req, resp);

    }
}
